package com.qf.intercepioirs;/*
 *徐韬
 *2021/10/28
 *15:10
 */

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.cache.MenuCache;
import com.qf.constons.resultCode;
import com.qf.dto.R;
import com.qf.exceptions.updateException;
import com.qf.pojo.TMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PipedReader;
import java.sql.BatchUpdateException;
import java.util.Map;

//@Component
public class UrlInterceptor implements HandlerInterceptor {
    private MenuCache menuCache;
    private ObjectMapper objectMapper;
@Autowired
    public void setObjectMapper(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    @Autowired
    public void setMenuCache(MenuCache menuCache) {
        this.menuCache = menuCache;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            String requestURI = request.getRequestURI();
            System.out.println(requestURI);
            TMenu menu = menuCache.getById(requestURI);
            System.out.println(menu.getStatus());
            boolean s  =menu.getStatus() != 1;
            if(s){
                throw new updateException("您访问得路径禁止访问",resultCode.NO_ROOT);
            }

            return !s;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }



}
